package com.yuma.tingshu.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yuma.tingshu.account.mapper.RechargeInfoMapper;
import com.yuma.tingshu.account.service.RechargeInfoService;
import com.yuma.tingshu.common.constant.SystemConstant;
import com.yuma.tingshu.common.util.AuthContextHolder;
import com.yuma.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuma.tingshu.vo.account.RechargeInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Override
    public Map<String, Object> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        RechargeInfo rechargeInfo = new RechargeInfo();
		rechargeInfo.setUserId(AuthContextHolder.getUserId());
		rechargeInfo.setOrderNo(IdWorker.getIdStr());
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        rechargeInfoMapper.insert(rechargeInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", rechargeInfo.getOrderNo());
        return map;
    }

	@Override
	public RechargeInfo getRechargeInfo(String orderNo) {
		return rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>()
				.eq(RechargeInfo::getOrderNo, orderNo));
	}
}
